package sg.edu.nus.dashboard.sql.control.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.IResponder;
	
	import sg.edu.nus.dashboard.main.model.AppModelLocator;
	import sg.edu.nus.dashboard.sql.control.business.ExeSQLDelegate;
	import sg.edu.nus.dashboard.sql.control.event.ExeSQLEvent;
	import sg.edu.nus.dashboard.util.SQLUtil;

	public class ExeSQLCommand implements ICommand, IResponder
	{
		private var model:AppModelLocator = AppModelLocator.getInstance();
		
		public function execute(event:CairngormEvent):void
		{
			var d:ExeSQLDelegate = new ExeSQLDelegate(this);
			var e:ExeSQLEvent = ExeSQLEvent(event);
			
			d.execute(e.sql);
		}
		
		public function result(data:Object):void
		{
			model.sql.resultArr = data.result;
			model.sql.resultStr = SQLUtil.translate(model.sql.resultArr);
		}
		
		public function fault(info:Object):void
		{
			Alert.show("Enter the right sql statement");
		}
	}
}